package dao.impl;

import dao.ConcernDao;
import domian.Collect;
import domian.Concern;
import domian.User;
import utils.BaseDAO;

import javax.servlet.http.Cookie;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 关注
 */
public class ConcernDaoImpl implements ConcernDao {
    @Override
    public Concern viewUid(Integer integer) {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        Concern concern = new Concern();
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建sql语句
            String sql="SELECT f_userId FROM file WHERE f_id="+integer;

            //获取sql语句
            ps = connection.prepareStatement(sql);

            //执行sql语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                Integer fUserId = resultSet.getInt("f_userId");
                concern.setFocus_uid(fUserId);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDAO.close(connection,ps,resultSet);
        }

        return concern;
    }

    @Override
    public Integer addConcern(Integer Uid,Integer Focus_uid) {
        String sql ="insert into focus(uid,focus_uid) values(?,?)";
        Object[] arr={Uid,Focus_uid};
        return BaseDAO.executeUpdate(sql,arr);
    }

    @Override
    public Integer cancelConcern(Integer focus_uid) {
        String sql="delete from focus where focus_uid = ?";
        Object[] arr={focus_uid};
        return BaseDAO.executeUpdate(sql,arr);
    }

    @Override
    public List<Concern> viewConcern(Integer uid,Integer rows, Integer pages) {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<Concern> list = new ArrayList<>();
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建sql语句
            String sql="SELECT focus_uid,headerimg,nickname FROM (SELECT focus_uid FROM focus WHERE uid="+uid+") a LEFT JOIN `user` ON a.`focus_uid`=`user`.`uid`";
            if (rows != null && pages != null) {
                sql += " LIMIT "+rows+","+pages;
            }
            //获取sql语句
            ps = connection.prepareStatement(sql);
            //执行sql语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                String headerimg = resultSet.getString("headerimg"); //关注用户头像
                String nickname = resultSet.getString("nickname");  //关注用户昵称
                Integer focus_uid = resultSet.getInt("focus_uid");  //被关注的用户id
                list.add(new Concern(focus_uid,headerimg,nickname));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDAO.close(connection,ps,resultSet);
        }
        return list;
    }

    @Override
    public List<Concern> fansConcern(Integer focus_uid) {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<Concern> list = new ArrayList<>();
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建sql语句
            String sql="SELECT uid FROM focus WHERE focus_uid="+focus_uid;

            //获取sql语句
            ps = connection.prepareStatement(sql);
            //执行sql语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                Integer uid = resultSet.getInt("uid");
                list.add(new Concern(uid));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDAO.close(connection,ps,resultSet);
        }
        return list;
    }

    @Override
    public List<Collect> concernMessage(Integer userId,Integer rows, Integer pages) {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<Collect> list = new ArrayList<>();
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建sql语句
            /*String sql="SELECT * FROM (SELECT * FROM (SELECT f_classId,f_name,f_integral FROM file WHERE f_userId="+userId+") a LEFT JOIN file_classify " +
                      " ON a.f_classId=file_classify.id) b LEFT JOIN file_classify  ON b.f_cid=file_classify.id";*/
            String sql="SELECT a.f_title,b.f_title,f_name,f_integral,f_id FROM `file` LEFT JOIN file_classify a ON f_classId=a.id " +
                    "JOIN file_classify b ON a.f_cid=b.id WHERE f_userId=?";

            if (rows != null && pages != null) {
                sql += " LIMIT "+rows+","+pages;
            }
            //获取sql语句
            ps = connection.prepareStatement(sql);
            ps.setInt(1,userId);
            //执行sql语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                String title = resultSet.getString("b.f_title"); //一级目录
                String titles = resultSet.getString("a.f_title");  //二级目录
                Integer integral = resultSet.getInt("f_integral");  //文件积分
                String fName = resultSet.getString("f_name");  //文件名字
                int fId = resultSet.getInt("f_id"); //文件id
                list.add(new Collect(fId,fName,integral,title,titles));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDAO.close(connection,ps,resultSet);
        }
        return list;
    }

    @Override
    public List<Concern> uidFollow(Integer uid) {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<Concern> list = new ArrayList<>();
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建sql语句
            String sql="SELECT focus_uid FROM focus WHERE uid=?";

            //获取sql语句
            ps = connection.prepareStatement(sql);
            ps.setInt(1,uid);
            //执行sql语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                Integer fId = resultSet.getInt("focus_uid");


                list.add(new Concern(fId));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDAO.close(connection,ps,resultSet);
        }
        return list;
    }

    @Override
    public List<User> documentUserMessageDao(Integer fid) {
        Connection connection =null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        List<User> list = new ArrayList<>();
        try {
            //获取连接
            connection = BaseDAO.getConnection();
            //创建sql语句
            String sql="SELECT * FROM (SELECT f_userId FROM file WHERE f_id=?) a LEFT JOIN `user` ON a.`f_userId`=`user`.`uid`";

            //获取sql语句
            ps = connection.prepareStatement(sql);
            ps.setInt(1,fid);
            //执行sql语句
            resultSet = ps.executeQuery();
            while (resultSet.next()){
                String headerimg = resultSet.getString("headerimg"); //用户头像
                String nickname = resultSet.getString("nickname");  //用户昵称


                list.add(new User(headerimg,nickname));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            //关闭资源
            BaseDAO.close(connection,ps,resultSet);
        }
        return list;
    }


}
